const os = require("os");

// 获取内网ip最后一位 127.0.0.1
let theLastIPNum = getLastIP() || ''
function getLastIP() {
  let interfaces = os.networkInterfaces()
  for (var devName in interfaces) {
    if(devName.includes('eth0')) {
      var iface = interfaces[devName];
      for (var i = 0; i < iface.length; i++) {
        var alias = iface[i];
        if (alias.family === 'IPv4' && alias.address) {
          let arr = alias.address.split('.')
          return Array.isArray(arr) && arr[arr.length -1]
        }
      }
    }
  }
}
// 获取当前进程id
let processID = process.pid
let xRequestId = `${theLastIPNum}-${processID}`;

module.exports = (req, res,next) => {
   // 阿里云资源请求返回404,避免资源回源
  let userAgent = req.headers['user-agent']
  if(userAgent && userAgent.indexOf('aliyun-oss-mirror') > -1) {
    return res.status(404).end()
  }
  res.removeHeader('X-Powered-By')
  const s = Date.now()
  req.xRequest = {time: s,id: xRequestId};
  return next()
}